package com.zx.zxblog.service.Impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zx.zxblog.entity.dto.BlogDTO;
import com.zx.zxblog.entity.pojo.Blog;
import com.zx.zxblog.entity.pojo.SysConfig;
import com.zx.zxblog.entity.query.BaseQuery;
import com.zx.zxblog.entity.vo.BlogVO;
import com.zx.zxblog.mapper.BlogMapper;
import com.zx.zxblog.mapper.SysConfigMapper;
import com.zx.zxblog.service.BlogService;
import com.zx.zxblog.service.SysConfigService;
import com.zx.zxblog.util.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * className=BlogServiceImpl
 * package=com.zx.zxblog.service.Impl
 * description:
 *
 * @Author:SKEZhi
 * @Create:2024/4/19 - 15:52
 * @Version: v1.0
 */

@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
        implements BlogService {
    @Autowired
    private BlogMapper blogMapper;

    @Override
    public PageInfo<Blog> pageBlogs(BaseQuery query) {
        PageHelper.startPage(query.getPageNum(), 10);
        PageHelper.orderBy("create_time desc");

        List<Blog> list = list();

        return new PageInfo<>(list);
    }

    @Override
    public List<Blog> searchBlog(String keyword) {
        if (StrUtil.isBlank(keyword)) {
            return null;
        }
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Blog::getTitle, keyword);
        return list(wrapper);
    }

    @Override
    public List<Blog> getBlogListDesc(){
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time"); // 替换"your_column"为你要倒序排序的字段名
        return blogMapper.selectList(queryWrapper);
    }

    @Override
    public R saveBlog(BlogDTO blogDTO) {
        Blog blog = new Blog();
        BeanUtils.copyProperties(blogDTO, blog);
        if (!save(blog)) {
            return R.error("保存文章信息失败!");
        }
        return saveBlogTags(blog.getId(), blogDTO.getTagIds()) ? R.success() : R.error("保存文章标签失败!");
    }

    public Boolean saveBlogTags(Long id, List<Long> tagIds) {
        return blogMapper.saveBlogTags(id, tagIds);
    }

    @Override
    public PageInfo<BlogVO> pageTagsBlog(long[] tagIds, Integer pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<BlogVO> blogList;
        // 如果没有标签Id
        if (ArrayUtil.isEmpty(tagIds)) {
            // 获取所有文章
            blogList  = getBaseMapper().getBlogList();
            PageInfo<BlogVO> pageInfo = new PageInfo<>(blogList);
            return pageInfo;

        }else{
            // 根据标签Id列表获取文章Id列表
            List<Long> blogIds = getBaseMapper().getBlogIdsByTagIds(tagIds);
            if (ArrayUtil.isEmpty(blogIds)) {
                return new PageInfo<>(new ArrayList<>());
            }
            //这一步目前仅用于获取分页信息，实际的数据列表还未设置。
            PageInfo pageInfo = new PageInfo(blogIds);
            //根据获取到的文章ID列表查询具体的博客列表 blogList。
            blogList = getBaseMapper().getBlogListByBlogIds(blogIds);
            //将查询得到的博客列表设置到之前创建的 pageInfo 对象中，这样 pageInfo 对象就包含了分页信息和具体的博客列表数据。
            pageInfo.setList(blogList);

            return pageInfo;
        }
    }

    @Override
    public BlogDTO getBlogDTO(Long id) {
        BlogVO blogVO = blogMapper.getBlogVO(id);
        BlogDTO blogDTO = new BlogDTO();
        BeanUtils.copyProperties(blogVO, blogDTO);
        if (blogVO.getTagList() == null) {
            return blogDTO;
        }
        //设置标签ID列表
        ArrayList<Long> tagIds = new ArrayList<>();
        blogVO.getTagList().forEach(tag -> {
            tagIds.add(tag.getId());
        });
        blogDTO.setTagIds(tagIds);

        return blogDTO;
    }

    @Override
    @Transactional
    public R updateBlog(BlogDTO blog) {
        //更新博客
        if (blogMapper.updateById(blog) <= 0) {
            return R.error("更新博客信息失败!");
        }
        if (!updateBlogTags(blog.getId(), blog.getTagIds())) {
            return R.error("更新博客标签失败!");
        }
        //更新博客的标签
        return  R.success() ;
    }

    public Boolean updateBlogTags(Long blogId, List<Long> tagIds) {
        //删除原有关联关系
        blogMapper.deleteAllBlogTags(blogId);
        //插入新的关联关系
        blogMapper.saveBlogTags(blogId, tagIds);

        return true;

    }

    @Override
    public PageInfo<BlogVO> pageAdminBlogs(BaseQuery query) {
        PageHelper.startPage(query.getPageNum(), 10);
        PageHelper.orderBy("create_time desc");

        List<BlogVO> list = blogMapper.pageAdminBlogs();

        return new PageInfo<>(list);

    }
}
